package cn.myallways.iglp.web.controller.mdm;


import com.allways.dubbo.mdm.beans.dataChangeLog.DataChangeLogEntity;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.service.dataChangeLog.IDataChangeLogService;
import com.myallways.iglp.cc.web.entity.ReturnVO;
import com.myallways.iglp.framework.base.controller.BaseController;
import com.myallways.iglp.framework.base.pagination.DataTableRequest;
import com.myallways.iglp.framework.base.pagination.DataTableResponse;
import com.myallways.iglp.framework.base.pagination.PageInfo;
import com.myallways.iglp.framework.base.pagination.PaginationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数据变更日志信息Controller
 * Created by weih on 2016-09-29.
 */
@Controller
@RequestMapping("/mdm/dataChangeLog")
public class DataChangeLogController extends BaseController {

    @Autowired
    IDataChangeLogService dataChangeLogService;
    /**
     * 跳转新增数据变更日志页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAddPage(){
        return "mdm/dataChangeLog/add";
    }
    /**
     * 新增数据变更日志信息
     * @param dataChangeLog 数据变更日志对象
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public void addDataChangeLog(@RequestBody DataChangeLog dataChangeLog,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        dataChangeLog.setCreateUser(getSessionUser().getUserId());
        dataChangeLog.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            dataChangeLogService.addDataChangeLog(dataChangeLog);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

    /**
     * 跳转编辑数据变更日志页面
     * @param dataChangeLogCode  数据变更日志代码
     * @return
     */
    @RequestMapping("/toEdit")
    public String toEditPage(String dataChangeLogCode,Model model){
        model.addAttribute("dataChangeLog", dataChangeLogService.getDataChangeLogByCode(dataChangeLogCode));
        return "mdm/dataChangeLog/edit";
    }

    /**
     * 编辑数据变更日志信息
     * @param dataChangeLog
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public void editDataChangeLog(@RequestBody DataChangeLog dataChangeLog,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        DataChangeLog oldDataChangeLog=dataChangeLogService.getDataChangeLogByCode(dataChangeLog.getDataChangeLogCode());
        oldDataChangeLog.setLogId(dataChangeLog.getLogId());
        oldDataChangeLog.setDataType(dataChangeLog.getDataType());
        oldDataChangeLog.setDataPk(dataChangeLog.getDataPk());
        oldDataChangeLog.setModifyTime(dataChangeLog.getModifyTime());
        oldDataChangeLog.setModifyType(dataChangeLog.getModifyType());
        oldDataChangeLog.setUpdateUser(getSessionUser().getUserId());
        oldDataChangeLog.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            dataChangeLogService.updateDataChangeLog(oldDataChangeLog);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response,vo);
    }

    /**
     * 删除数据变更日志信息
     * @param dataChangeLog 数据变更日志对象
     * @return
     */
    @RequestMapping(value= "/delete", method = RequestMethod.POST)
    public void deleteDataChangeLog(@RequestBody DataChangeLog dataChangeLog,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        dataChangeLog.setUpdateUser(getSessionUser().getUserId());
        dataChangeLog.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            dataChangeLogService.deleteDataChangeLog(dataChangeLog);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

    /**
     * 跳转分页查询界面
     * @return
     */
    @RequestMapping("/list")
    public String toListPage(){
        return "mdm/dataChangeLog/list";
    }

    /**
     * 分页查询数据
     * @param dataChangeLog 数据变更日志对象
     * @param dtrequest 页码
     */
    @RequestMapping("/getListData")
    @ResponseBody
    public DataTableResponse<DataChangeLog> getDataChangeLogData(DataChangeLog dataChangeLog ,DataTableRequest dtrequest){
        int cnt = (int)dataChangeLogService.getDataChangeLogCount(dataChangeLog);
        List<DataChangeLog> list = dataChangeLogService.queryDataChangeLogs(dataChangeLog, dtrequest.getiDisplayStart(), dtrequest.getiDisplayStart() + dtrequest.getiDisplayLength());
        PageInfo pi = new PageInfo();

        pi.setTotal(cnt);
        pi.setRows(dtrequest.getiDisplayLength());
        pi.setPage(dtrequest.getCurrentPage());
        PaginationResult<DataChangeLog> result = new PaginationResult<DataChangeLog>(list, pi);
        return new DataTableResponse<DataChangeLog>(dtrequest.getsEcho(),result);
    }

}
